<?php

class SiteController extends Controller
{
	private $perpage = 9;


	public $layout='//layouts/site';

	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),			
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// load sections
		$items = Sections::model()->findAll(array(
			'select'=>'id, name',
			'condition'=>'enabled = 1',
			'order'=>'order_id ASC',
		));

		$sections = array();
		foreach ($items as $section){
			$sections[] = array(
				'label' => $section->name,
				'url' => '?r=site/view&id_section=' . $section->id,
			);
		}

		// load mainpage
		$mainpage = Articles::model()->findByPk( 1 );

		// renders the view file 'protected/views/site/index.php'		
		$this->render('index', array(
			'breadcrumbs' => $breadcrumbs,
			'menu' => $sections,
			'mainpage' => $mainpage,
		));
	}


	/**
	 * view FIRM
	 */
	public function actionView()
	{
		if ($_GET['id_section']) {
			$section = Sections::model()->findByPk((int)$_GET['id_section']);
			$subitems = Firms::model()->findAll(array(
				'with'=>array('icon'),
				'select'=>'t.id, t.title, t.id_section',
				'condition'=>'t.id_section = '.(int)$_GET['id_section'].' AND t.enabled = 1',
				'order'=>'t.order_id ASC',
				));
		}
		if ($_GET['id']) {
			$firm = Firms::model()->findByPk((int)$_GET['id']);
		}

		// breadcrumbs
		$breadcrumbs = array();
		if ($_GET['id_section']) {
			$breadcrumbs[$section->name] = array( 'site/view', 'id_section' => $section->id );
		}
		if ($_GET['id']) {			
			$breadcrumbs[$firm->title] = array( 'site/view', 'id_section' => $section->id, 'id' => $firm->id );
		}

		// left menu
		// load firms
		if ($_GET['id_section']) {			
			$firms = array();
			foreach ($subitems as $item){
				$firms[] = array(
					'label' => $item->title,
					'url' => '?r=site/view&id_section=' . (int)$_GET['id_section'] . '&id=' . $item->id,
					'itemOptions'=>$item->id == $_GET['id'] ? array('class'=> 'active') : array(),
				);
			}
		}

		// load sections
		$items = Sections::model()->findAll(array(
			'select'=>'id, name',
			'condition'=>'enabled = 1',
			'order'=>'order_id ASC',
			));

		$sections = array();
		foreach ($items as $sect){
			$sections[] = array(
				'label' => $sect->name,
				'url' => '?r=site/view&id_section=' . $sect->id,
				'items' => ($sect->id == $_GET['id_section']) ? $firms : array(),
			);
		}

		// load content
		if ($_GET['id']) {
			// firm

			$this->render('viewfirm', array(
				'breadcrumbs' => $breadcrumbs,
				'menu' => $sections,
				'firm' => $firm,
			));

		} elseif ($_GET['id_section']) {
			// section

			$subitems = Firms::model()->findAll(array(
				'with'=>array('icon'),
				'select'=>'t.id, t.title, t.id_section',
				'condition'=>'t.id_section = '.(int)$_GET['id_section'].' AND t.enabled = 1',
				'order'=>'t.order_id ASC',
				'offset'=> $_GET['st'] ? $_GET['st'] : 0,
				'limit'=> $this->perpage,
			));

			$total = Firms::model()->count( 't.id_section = '.(int)$_GET['id_section'].' AND t.enabled = 1');
			
			$this->render('viewsection', array(
				'breadcrumbs' => $breadcrumbs,
				'menu' => $sections,
				'section' => $section,
				'items' => $subitems,
				'pages' => ceil( $total / $this->perpage ),
				'page' => $_GET['st'] ? ($_GET['st'] / $this->perpage) + 1 : 1,
				'perpage' => $this->perpage,
			));

		} else {
			$this->redirect(array('index'));
		}
		
		
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}